---
title: "Replication"
output: pdf_document
date: "2022-11-05"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Basic Setup
```{r message=F}
#Check to see if necessary packages are installed.
#If not, the packages will be installed automatically.

packages <- c("plm", "rio", "lmtest", "stargazer", "sandwich", "ggplot2")
new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)
rm(packages, new.packages)

#Load packages

require(rio)
require(plm)
require(sandwich)
require(lmtest)
require(stargazer)
require(ggplot2)
rm(list=ls())


##
##Load data
##

bio <- import("bio_data.csv", encoding = "UTF-8")
career <- import("career_data.csv", encoding = "UTF-8")
positions <- import("key_positions_data.csv", encoding = "UTF-8")

##
##Subset data to PLA officers holding key positions during relevant period:
##Deng era to onset of reforms
##

positions <- positions[positions$start_date_year>1978&positions$start_date_year<2018,]
bio <- bio[bio$name %in% positions$name,]
career <- career[career$unique_id %in% bio$unique_id,]

```


# Variable Creation
```{r}
positions$general_rank <- 0
positions$general_rank[which(positions$position_id %in% c(100, 201, 301, 303, 401, 403, 501, 503, 601, 603, 701, 703, 801, 803, 1201,  
                                                          1203, 1301, 1303,1401, 1403, 1501, 1503, 1601, 1603, 1701, 1703, 1801, 1803,
                                                          1901, 1903, 2001, 2003, 2101, 2103, 2201, 2203, 2301, 2303, 2401, 2403,
                                                          2501, 2503, 2601, 2603, 2701, 2703))] <- 1


##
##Code individuals who ever held a position of MR Region Leader or Equivalent
##

bio$general <- 0
bio$general[bio$name %in% positions$name[positions$general_rank==1]] <- 1

##
##Indicator for whether an officer served in the Central Military Commmission
##

bio$cmc[is.na(bio$cmc)] <- 0



##
##Code individuals who achived the rank of full general (上将)
##Note that after initial promotions in the 1950s, this system was inactive until 1988
##

bio$general_rank <- 0
bio$general_rank[which(bio$shangjiang_year>1949)] <- 1


##
##Variable for whether individual was a Central Committee full member in the 12th to 18th party congresses
##

bio$cc_full_member <- NA

for(i in 1:nrow(bio)){
  bio$cc_full_member[i] <- length(which(bio[,paste0("cc", 12:18)][i,]<3000))
}

bio$cc_full_member[bio$cc_full_member>1] <- 1


##
##Variable for whether individual was a Central Committee full member in the 12th to 18th party congresses
##

bio$cc_full_alt_member <- NA

for(i in 1:nrow(bio)){
  bio$cc_full_alt_member[i] <- length(which(bio[,paste0("cc", 12:18)][i,]<4000))
}

bio$cc_full_alt_member[bio$cc_full_alt_member>1] <- 1



##
##Variable for whether individual was a Politburo member in the 12th to 18th party congresses
##

bio$politburo_member <- NA

for(i in 1:nrow(bio)){
  bio$politburo_member[i] <- length(which(bio[,paste0("cc", 12:18)][i,]<2000))
}

bio$politburo_member[bio$politburo_member>1] <- 1


##
##Variable for whether individuals were promoted to general in the Xi Era 
##Restricted to generals active in the Xi Era
##

bio$general_xi <- NA
bio$general_xi[bio$name %in% positions$name[which(positions$start_date_year>=2012)]] <- 0
bio$general_xi[which(bio$shangjiang_year>2012&bio$shangjiang_year<2018)] <- 1


##
##Variable for whether individuals were promoted to general in the Hu Era
##Restricted to generals active in the Hu Era
##

bio$general_hu <- NA
bio$general_hu[bio$name %in% positions$name[which(positions$start_date_year>=2004&positions$start_date_year<2012)]] <- 0
bio$general_hu[which(bio$shangjiang_year>=2004&bio$shangjiang_year<2012)] <- 1

##
##Variable for whether individuals were promoted to general in the Jiang Era
##Restricted to generals active in the Jiang Era
##

bio$general_jiang <- NA
bio$general_jiang[bio$name %in% positions$name[which(positions$start_date_year>1989&positions$start_date_year<2004)]] <- 0
bio$general_jiang[which(bio$shangjiang_year>1989&bio$shangjiang_year<=2004)] <- 1

##
##Variable for whether individuals were promoted to general in the Deng Era
##Restricted to generals active in the Deng Era
##

bio$general_deng <- NA
bio$general_deng[bio$name %in% positions$name[which(positions$start_date_year>=1978&positions$start_date_year<=1989)]] <- 0
bio$general_deng[which(bio$shangjiang_year==1988)] <- 1

##
##Variable for whether individuals were promoted to the Central Committee in the Xi Era 
##Restricted to generals active in the Xi Era
##

bio$cc_xi <- NA
bio$cc_xi[bio$name %in% positions$name[which(positions$start_date_year>=2012&positions$start_date_year<2018)]] <- 0
bio$cc_xi[which(bio$cc18<3000)] <- 1

##
##Variable for whether individuals were promoted to the Central Committee in the Hu Era 
##Restricted to generals active in the Hu Era
##

bio$cc_hu <- NA
bio$cc_hu[bio$name %in% positions$name[which(positions$start_date_year>=2004&positions$start_date_year<2012)]] <- 0
bio$cc_hu[which(bio$cc16<3000|bio$cc17<3000)] <- 1

##
##Variable for whether individuals were promoted to the Central Committee in the Jiang Era 
##Restricted to generals active in the Jiang Era
##

bio$cc_jiang <- NA
bio$cc_jiang[bio$name %in% positions$name[which(positions$start_date_year>1989&positions$start_date_year<2004)]] <- 0
bio$cc_jiang[which(bio$cc14<3000|bio$cc15<3000)] <- 1

##
##Variable for whether individuals were promoted to the Central Committee in the Deng Era 
##Restricted to generals active in the Deng Era
##

bio$cc_deng <- NA
bio$cc_deng[bio$name %in% positions$name[which(positions$start_date_year<=1989)]] <- 0
bio$cc_deng[which(bio$cc12<3000|bio$cc13<3000)] <- 1


##
##Variable for whether individuals were promoted to the Central Military Commission in the Xi Era 
##Restricted to generals active in the Xi Era
##

bio$cmc_xi <- NA
bio$cmc_xi[bio$name %in% positions$name[which(positions$start_date_year>=2012)]] <- 0
bio$cmc_xi[which(bio$name %in% positions$name[which(positions$position_id==100&positions$start_date_year>=2012)])] <- 1

##
##Variable for whether individuals were promoted to the Central Military Commission in the Hu Era 
##Restricted to generals active in the Hu Era
##

bio$cmc_hu <- NA
bio$cmc_hu[bio$name %in% positions$name[which(positions$start_date_year>=2004&positions$start_date_year<2012)]] <- 0
bio$cmc_hu[which(bio$name %in% positions$name[which(positions$position_id==100&positions$start_date_year>=2004&positions$start_date_year<2012)])] <- 1

##
##Variable for whether individuals were promoted to the Central Military Commission in the Jiang Era 
##Restricted to generals active in the Jiang Era
##

bio$cmc_jiang <- NA
bio$cmc_jiang[bio$name %in% positions$name[which(positions$start_date_year>=1989&positions$start_date_year<2004)]] <- 0
bio$cmc_jiang[which(bio$name %in% positions$name[which(positions$position_id==100&positions$start_date_year>=1989&positions$start_date_year<2004)])] <- 1

##
##Variable for whether individuals were promoted to the Central Military Commission in the Deng Era 
##Restricted to generals active in the Deng Era
##

bio$cmc_deng <- NA
bio$cmc_deng[bio$name %in% positions$name[which(positions$start_date_year>=1989&positions$start_date_year<2004)]] <- 0
bio$cmc_deng[which(bio$name %in% positions$name[which(positions$position_id==100&positions$start_date_year>=1978&positions$start_date_year<1989)])] <- 1



##########################################################
##Explanatory Variables: Connections to paramount leader##
##########################################################


######
#####Code service in different regions in the career data
#####

##Nanjing Military Region

career$nanjing.mr <- 0
career$nanjing.mr[grep("南京军区", career$institution)] <- 1

##Chengdu Military Region

career$chengdu.mr <- 0
career$chengdu.mr[grep("成都军区", career$institution)] <- 1



##Guizhou Military District

career$guizhou.md <- 0
career$guizhou.md[grep("贵州省军区", career$institution)] <- 1
career$guizhou.md[grep("贵州军区", career$institution)] <- 1

##Tibet Military District 

career$xizang.md <- 0
career$xizang.md[grep("西藏军区", career$institution)] <- 1
career$xizang.md[grep("西藏总队", career$institution)] <- 1


##Zhejiang Military District

career$zhejiang.md <- 0
career$zhejiang.md[grep("浙江军区", career$institution)] <- 1
career$zhejiang.md[grep("浙江省军区", career$institution)] <- 1


##Fujian Military District, Including 31st Group Army

career$fujian.md <- 0
career$fujian.md[grep("空军第八军", career$institution)] <- 1
career$fujian.md[grep("第十三集团", career$institution)] <- 1
career$fujian.md[grep("第31军", career$institution)] <- 1
career$fujian.md[grep("福建", career$institution)] <- 1

##Fuzhou Military Sub-District and related institutions, including 31st Group Army

career$fuzhou.msd <- 0
career$fuzhou.msd[grep("福州", career$institution)] <- 1
career$fuzhou.msd[grep("空军第八军", career$institution)] <- 1

##Zhejiang Military District

career$shanghai.garrison <- 0
career$shanghai.garrison[grep("上海", career$institution)] <- 1


##General Staff Department of the Central Military Commission

career$general_staff <- 0
career$general_staff[grep("军事委员会, 总参谋部", career$institution)] <- 1


######
#####Code connections to each paramount leader
#####

##Connections to Deng Xiaoping based on service in General Staff office 
##when Deng was Chief of Staff or Second Field Army, of which Deng was Commissar

bio$deng.network <- 0
bio$deng.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1980&career$end_year>=1975&career$general_staff==1)]] <- 1
bio$deng.network[which(bio$second_field==1)] <- 1

##Connections to Jiang Zemin
##Based on concurrent service in Shanghai

bio$jiang.network <- 0
bio$jiang.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1989&career$end_year>=1985&career$shanghai.garrison==1)]] <- 1
bio$jiang.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1989&career$end_year>=1985&career$nanjing.mr==1)]] <- 1

##Connections to Hu Jintao
##Based on concurrent service in Guizhou and Tibet

bio$hu.network <- 0
bio$hu.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1988&career$end_year>=1985&career$guizhou.md==1)]] <- 1
bio$hu.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1992&career$end_year>=1985&career$chengdu.mr==1)]] <- 1
bio$hu.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1992&career$end_year>=1989&career$xizang.md==1)]] <- 1

##Connections to Xi Jinping
##Based on concurrent service in Shanghai, Zhejiang, Fujian, Fuzhou

bio$xi.network <- 0
bio$xi.network[bio$unique_id %in% career$unique_id[which(career$start_year<=2007&career$end_year>1999&career$nanjing.mr==1)]] <- 1
bio$xi.network[bio$unique_id %in% career$unique_id[which(career$start_year<=2007&career$end_year>2002&career$zhejiang.md==1)]] <- 1
bio$xi.network[bio$unique_id %in% career$unique_id[which(career$start_year<=2002&career$end_year>1999&career$fujian.md==1)]] <- 1
bio$xi.network[bio$unique_id %in% career$unique_id[which(career$start_year<=1996&career$end_year>1990&career$fuzhou.msd==1)]] <- 1
bio$xi.network[bio$unique_id %in% career$unique_id[which(career$start_year<=2007&career$end_year>=2007&career$shanghai.garrison==1)]] <- 1

#Zhang Youxia's father fought alongside Xi Jinping's father in the revolution
#and Xi and Zhang reportedly have known each other since they were children.
#Including or excluding Zhang Youxia does not alter the results but is included for completeness.

bio$xi.network[bio$name=="张又侠"] <- 1

##Alternative measures of connections to Xi excluding Military Regions

bio$xi.network.alt <- 0
bio$xi.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=2007&career$end_year>2002&career$zhejiang.md==1)]] <- 1
bio$xi.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=2002&career$end_year>1999&career$fujian.md==1)]] <- 1
bio$xi.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=1996&career$end_year>1990&career$fuzhou.msd==1)]] <- 1
bio$xi.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=2007&career$end_year>=2007&career$shanghai.garrison==1)]] <- 1

##Alternative measures of connections to Jiang excluding Military Regions

bio$jiang.network.alt <- 0
bio$jiang.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=1989&career$end_year>=1985&career$shanghai.garrison==1)]] <- 1

##Alternative measures of connections to Hu excluding Military Regions

bio$hu.network.alt <- 0
bio$hu.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=1988&career$end_year>=1985&career$guizhou.md==1)]] <- 1
bio$hu.network.alt[bio$unique_id %in% career$unique_id[which(career$start_year<=1992&career$end_year>=1989&career$xizang.md==1)]] <- 1



######
#####Code connections to each paramount leader
#####

##
##Connected to any CMC chairman
##

bio$cmc_chair_connection <- 0
bio$cmc_chair_connection[which(bio$xi.network==1|bio$jiang.network==1|bio$hu.network==1|bio$deng.network==1)] <- 1

##
##Connected to any CMC chairman excluding military regions
##

bio$cmc_chair_connection_alt <- 0
bio$cmc_chair_connection_alt[which(bio$xi.network.alt==1|bio$jiang.network.alt==1|bio$hu.network.alt==1|bio$deng.network==1)] <- 1

##
##Connections to current CMC chairman
##Excludes connections when general served on Central Committee during prior CMC chair
##




bio$cmc_chair_connection_current <- bio$cmc_chair_connection
bio$cmc_chair_connection_current[which(bio$xi.network==1&bio$hu.network==0&bio$jiang.network==0&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000|bio$cc14<3000|bio$cc15<3000|bio$cc16<3000|bio$cc17<3000)&(is.na(bio$cc18)&is.na(bio$cc19)))] <- NA
bio$cmc_chair_connection_current[which(bio$xi.network==0&bio$hu.network==1&bio$jiang.network==0&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000|bio$cc14<3000|bio$cc15<3000)&(is.na(bio$cc16)&is.na(bio$cc17)))] <- NA
bio$cmc_chair_connection_current[which(bio$xi.network==0&bio$hu.network==0&bio$jiang.network==1&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000)&(is.na(bio$cc14)&is.na(bio$cc15)))] <- NA
bio$cmc_chair_connection_current[which(bio$xi.network==0&bio$hu.network==0&bio$jiang.network==0&bio$deng.network==1&
                                         (bio$cc15<3000|bio$cc16<3000)&(is.na(bio$cc12)&is.na(bio$cc13)))] <- NA




##
##Connections to current CMC chairman excluding military regions
##(Excludes connections when general served on Central Committee during prior or subsequent CMC chair)
##


bio$cmc_chair_connection_current_alt <- bio$cmc_chair_connection_alt
bio$cmc_chair_connection_current_alt[which(bio$xi.network.alt==1&bio$hu.network.alt==0&bio$jiang.network.alt==0&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000|bio$cc14<3000|bio$cc15<3000|bio$cc16<3000|bio$cc17<3000)&(is.na(bio$cc18)&is.na(bio$cc19)))] <- NA
bio$cmc_chair_connection_current_alt[which(bio$xi.network.alt==0&bio$hu.network.alt==1&bio$jiang.network.alt==0&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000|bio$cc14<3000|bio$cc15<3000)&(is.na(bio$cc16)&is.na(bio$cc17)))] <- NA
bio$cmc_chair_connection_current_alt[which(bio$xi.network.alt==0&bio$hu.network.alt==0&bio$jiang.network.alt==1&bio$deng.network==0&
                                         (bio$cc12<3000|bio$cc13<3000)&(is.na(bio$cc14)&is.na(bio$cc15)))] <- NA
bio$cmc_chair_connection_current_alt[which(bio$xi.network.alt==0&bio$hu.network.alt==0&bio$jiang.network.alt==0&bio$deng.network==1&
                                         (bio$cc15<3000|bio$cc16<3000)&(is.na(bio$cc12)&is.na(bio$cc13)))] <- NA

######################################################
##Explanatory Variables: Measures of Professionalism##
######################################################

##
##Combat Experience in the Korean, Indian, and Vietnam conflicts
##


bio$combat_post_1949 <- 0
bio$combat_post_1949[grep("Korean", bio$combat)] <- 1
bio$combat_post_1949[grep("Indian", bio$combat)] <- 1
bio$combat_post_1949[grep("Vietnam", bio$combat)] <- 1

##
##College experience, including military academies
##

bio$college <- 0
bio$college[which(bio$education>1)] <- 1

##
##Post-graduate experience
##

bio$postgrad <- 0
bio$postgrad[which(bio$education>2)] <- 1



#########################
##Additional covariates##
#########################

##
##Long March participant
##

bio$participated_long_march[is.na(bio$participated_long_march)] <- 0


##
##Princeling
##


bio$parent_CCP_leader[is.na(bio$parent_CCP_leader)] <- 0


##
##Served in political commissar role
##


bio$commissar <- 0
bio$commissar[bio$name %in% positions$name[positions$position_id %in% c(303, 304, 403, 404, 503, 504, 603, 604, 703, 704, 803, 804,
                                                                        1201, 1202, 1301, 1302, 1401, 1402, 1501, 1502, 1601, 1602,
                                                                        1701, 1702, 1801, 1802, 1901, 1902, 2001, 2002, 2101, 2202,
                                                                        2301, 2302, 2401, 2402, 2501, 2502, 2601, 2602)]] <- 1

##
##Ethnic minority
##


bio$minority <- 0
bio$minority[which(bio$ethnicity!="汉族"&bio$ethnicity!="")] <- 1


##
##Rural birth
##


bio$rural[is.na(bio$rural)] <- 0
#bio$rural[which(bio$birth_province=="")] <- NA


##
##Decade of birth year
##


bio$cohort_decade <- as.factor(substr(bio$birth_year, 3, 3))


##
##Decade of birth year
##


bio$post_deng <- 0
bio$post_deng[bio$name %in% positions$name[which(positions$start_date_year>1989|positions$end_date_year>1989)]] <- 1


################################
##Domestic and foreign threats##
################################

##
##Create officer-year panel
##


##
##Create variable for first year someone reaches a military 
##region leader (正大军区职)-level position
##

bio$general_year <- NA
for(i in 1:nrow(bio)){
  if(sum(positions$start_date_year[which(positions$general_rank==1&positions$name==bio$name[i])], na.rm = T)){
  bio$general_year[i] <- min(positions$start_date_year[which(positions$general_rank==1&positions$name==bio$name[i])])
  }
}



##
##Create variable for the first year someone appears in the dataset 
##as a military region deputy leader (副大军区职) or equivalent
##

bio$first_year <- NA
for(i in 1:nrow(bio)){
  if(sum(positions$start_date_year[which(positions$name==bio$name[i])], na.rm=T)>0){
  bio$first_year[i] <- min(positions$start_date_year[which(positions$name==bio$name[i])])}
}


##
##Create variable for the last year someone appears in the dataset 
##


bio$last_year <- NA
for(i in 1:nrow(bio)){
  if(sum(positions$end_date_year[which(positions$name==bio$name[i])], na.rm=T)>0){
    bio$last_year[i] <- max(positions$end_date_year[which(positions$name==bio$name[i])], na.rm=TRUE)}
}



##
##Create panel dataset
##Time-invariant covariates will be repeated for each row
##

panel_year <- NULL
for(i in 1:nrow(bio)){
    if(is.na(bio$last_year[i])==FALSE){
    year <- seq(bio$first_year[i], bio$last_year[i])
    temp <- cbind(bio[rep(i, length(year)),], year)
    panel_year <- rbind(panel_year, temp)
    rm(temp, year)
  }
}


##
##Create a time-varying indicator for each leader's term in office as CMC Chair
##

panel_year$xi.office <- 0
panel_year$xi.office[panel_year$year>2011] <- 1

panel_year$hu.office <- 0
panel_year$hu.office[panel_year$year<2012&panel_year$year>2003] <- 1

panel_year$jiang.office <- 0
panel_year$jiang.office[panel_year$year<2004&panel_year$year>1989] <- 1

panel_year$deng.office <- 0
panel_year$deng.office[panel_year$year<1990] <- 1



##
##Create a time-varying indicator for whether an individual is connected 
##to a leader while that leader is the current CMC Chair
##

panel_year$xi.varying <- panel_year$xi.office*panel_year$xi.network
panel_year$hu.varying <- panel_year$hu.office*panel_year$hu.network
panel_year$jiang.varying <- panel_year$jiang.office*panel_year$jiang.network
panel_year$deng.varying <- panel_year$deng.office*panel_year$deng.network



panel_year$xi.varying.alt <- panel_year$xi.office*panel_year$xi.network.alt
panel_year$hu.varying.alt <- panel_year$hu.office*panel_year$hu.network.alt
panel_year$jiang.varying.alt <- panel_year$jiang.office*panel_year$jiang.network.alt




##
##Create a time-varying indicator for whether an individual is connected 
##to the current CMC Chair
##


panel_year$cmc_connection_varying <- panel_year$xi.varying+panel_year$hu.varying+panel_year$deng.varying+panel_year$jiang.varying

panel_year$cmc_connection_varying_alt <- panel_year$xi.varying.alt+panel_year$hu.varying.alt+panel_year$deng.varying+panel_year$jiang.varying


##
##Create measure for generals by year
##


panel_year$general_varying <- 0
panel_year$general_varying[which(panel_year$general_year<=panel_year$year)] <- 1



##
##Create time-varying measure for age
##

panel_year$age <- panel_year$year-panel_year$birth_year


####
####Create indicators for periods of foreign threat
####


##
##Indicator for post-2000 increased tensions with United States
##

panel_year$foreign <- 0
panel_year$foreign[panel_year$year==2000|panel_year$year==2001|panel_year$year==2002] <- 1

##
##Alternate indicator for post-2000 increased tensions with United States
##plus conflict with Vietnam
##


panel_year$foreign2 <- 0
panel_year$foreign2[panel_year$year==1979|panel_year$year==1980|panel_year$year==1981|panel_year$year==2000|panel_year$year==2001|panel_year$year==2002] <- 1

##
##Alternate indicator for foreign threat
##excluding certain years
##

panel_year$foreign2000 <- 0
panel_year$foreign2000[panel_year$year==2001|panel_year$year==2002] <- 1

panel_year$foreign2001 <- 0
panel_year$foreign2001[panel_year$year==2000|panel_year$year==2002] <- 1

panel_year$foreign2002 <- 0
panel_year$foreign2002[panel_year$year==2000|panel_year$year==2001] <- 1

panel_year$foreign2003 <- 0
panel_year$foreign2003[panel_year$year==2000|panel_year$year==2001|panel_year$year==2002] <- 1




####
####Create indicators for periods of domestic threat
####


##
##Indicator for elite threats in the wake of Tiananmen and the Bo incident
##


panel_year$domestic <- 0
panel_year$domestic[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1


##
##Alternate indicator for elite threats including Deng's outster of Hua Guofeng and consolidation of power
##


panel_year$domestic2 <- 0
panel_year$domestic2[panel_year$year==1979|panel_year$year==1980|panel_year$year==1981|panel_year$year==1982|panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1

##
##Alternate indicator for domestic threat
##excluding certain years
##


panel_year$domestic1990 <- 0
panel_year$domestic1990[panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1


panel_year$domestic1991 <- 0
panel_year$domestic1991[panel_year$year==1990|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1


panel_year$domestic1992 <- 0
panel_year$domestic1992[panel_year$year==1990|panel_year$year==1991|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1

panel_year$domestic1993 <- 0
panel_year$domestic1993[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1

panel_year$domestic2012 <- 0
panel_year$domestic2012[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2013|panel_year$year==2014|panel_year$year==2015] <- 1

panel_year$domestic2013 <- 0
panel_year$domestic2013[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2014|panel_year$year==2015] <- 1

panel_year$domestic2014 <- 0
panel_year$domestic2014[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2015] <- 1

panel_year$domestic2015 <- 0
panel_year$domestic2015[panel_year$year==1990|panel_year$year==1991|panel_year$year==1992|panel_year$year==1993|panel_year$year==2012|panel_year$year==2013|panel_year$year==2014] <- 1


##
##Create ranked education year variable
##


panel_year$education_level <- panel_year$education
panel_year$education_level[is.na(panel_year$education_level)] <- 1



##
##Create pdata.frame object
##

panel_year <- pdata.frame(panel_year, index = c("unique_id", "year"), drop.index = F, row.names = T)
```

# Main Results
```{r warning=FALSE}
####################################
##Table 1: Correlates of Promotion##
####################################

lm1 <- lm(general~cmc_chair_connection_current, data=bio)
lm2 <- lm(general~combat_post_1949, data=bio)
lm3 <- lm(general~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, data=bio)
lm4 <- lm(cmc~cmc_chair_connection_current, data=bio)
lm5 <- lm(cmc~combat_post_1949, data=bio)
lm6 <- lm(cmc~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, data=bio)

rob.fit1        <- coeftest(lm1, function(x) vcovHC(x, type="HC3"))
rob.fit2        <- coeftest(lm2, function(x) vcovHC(x, type="HC3"))
rob.fit3        <- coeftest(lm3, function(x) vcovHC(x, type="HC3"))
rob.fit4        <- coeftest(lm4, function(x) vcovHC(x, type="HC3"))
rob.fit5        <- coeftest(lm5, function(x) vcovHC(x, type="HC3"))
rob.fit6        <- coeftest(lm6, function(x) vcovHC(x, type="HC3"))

stargazer(lm1, lm2, lm3, lm4, lm5, lm6, type = "latex", 
          se = list(rob.fit1[,"Std. Error"], rob.fit2[,"Std. Error"],
                    rob.fit3[,"Std. Error"], rob.fit4[,"Std. Error"],
                    rob.fit5[,"Std. Error"], rob.fit6[,"Std. Error"]), 
          omit=c("cohort_decade"),
          label = "main_results", 
          omit.stat=c("f", "ser"),
          dep.var.labels = c("Promoted to General", "Promoted to CMC"),
          add.lines=list(c("Birth cohort fixed effects", "", "", "$\\checkmark$", "", "", "$\\checkmark$")),
          title = "Promotion to full general-level position and to the CCP Central Military Commission",
          star.char = c("+", "*", "**"),
          notes.append=FALSE,
          notes = c("PLA officers connected to the top civilian leader or with combat experience are more likely to be promoted. Ordinary-least squares regression on cross-sectional dataset of PLA officers in the post-1978 period. The outcomes are a binary indicator for promotion to a general-grade position (military region commander or commissar and above) and promotion to the the Central Military Commission (CMC). Robust standard errors are in parentheses.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          covariate.labels = c("Career Tie to Paramount Leader", "Combat Experience, Post-1949", "College-Level Education", "Long March Participant", "Political Commissar Experience", "Ethnic Minority", "Princeling", "Rural Birth"),
          out = "Table_1.tex")



#############################
##Table 2: Domestic threats##
#############################


lm1 <- plm(general_varying~cmc_connection_varying*domestic+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~cmc_connection_varying*domestic+age+combat_post_1949*domestic+college*domestic+participated_long_march*domestic+commissar*domestic+minority*domestic+parent_CCP_leader*domestic+rural*domestic+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~cmc_connection_varying*domestic+age+combat_post_1949*domestic+college*domestic+participated_long_march*domestic+commissar*domestic+minority*domestic+parent_CCP_leader*domestic+rural*domestic+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~cmc_connection_varying*domestic+age+combat_post_1949*domestic+college*domestic+participated_long_march*domestic+commissar*domestic+minority*domestic+parent_CCP_leader*domestic+rural*domestic++as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant", "age", "college", "participated_long_march", "commissar", "combat_post_1949", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("domestic_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Domestic Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
         covariate.labels = c("Career Tie to Current CMC Chairman", "Period of Domestic Threat", "Career Tie to Chairman X Domestic Threat"),
         title = "Promotion during periods of domestic threat.",
         star.char = c("+", "*", "**"),
         notes.append=FALSE,
         notes = c("PLA officers with a tie to the sitting top leader are especially likely to be promoted during periods of domestic threat, here operationalized as the period after the 1989 protests and after the 2012 Bo Xilai incident. See Table A13, p. A18 and Table A16, p. A21 for alternate operationalizations. Two-way fixed effects regression on a panel dataset of PLA officers with yearly observations. Outcome is promotion to general-grade position (military region commander or commissar and above). Robust standard errors are clustered by individual.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_2.tex")




############################
##Table 3: Foreign threats##
############################


lm1 <- plm(general_varying~combat_post_1949*foreign+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~combat_post_1949*foreign+age+cmc_connection_varying*foreign+college*foreign+participated_long_march*foreign+commissar*foreign+combat_post_1949*foreign+parent_CCP_leader*foreign+rural*foreign+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~combat_post_1949*foreign+age+cmc_connection_varying*foreign+college*foreign+participated_long_march*foreign+commissar*foreign+combat_post_1949*foreign+parent_CCP_leader*foreign+rural*foreign+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~combat_post_1949*foreign+age+cmc_connection_varying*foreign+college*foreign+participated_long_march*foreign+commissar*foreign+combat_post_1949*foreign+parent_CCP_leader*foreign+rural*foreign+as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))



stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant","age" , "college", "participated_long_march", "commissar", "cmc_connection_varying", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("foreign_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Foreign Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
          covariate.labels = c("Period of Foreign Threat", "Combat Experience X Foreign Threat"),
          title = "Promotion during periods of foreign threat.",
          star.char = c("+", "*", "**"),
          notes.append=FALSE,
          notes = c("Officers with combat experience are especially likely to promoted during periods of foreign threat, here operationalized as the period of rising tensions with the United States in the early 2000s. See Table A14, p. A19 and Table A17, p. A22 for alternate operationalizations.  Two-way fixed effects regression on a panel dataset of PLA officers with yearly observations. Outcome is promotion to general-grade position (military region commander or commissar and above). Robust standard errors are clustered by individual. $^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_3.tex")
```


# Detecting Model Misspecifications
```{r message=FALSE}
library(RobustSE)
library(broom)
library(tidyverse)
library(kableExtra)
glm1 <- glm(general~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, family = gaussian, data=bio)
logit1 <- glm(general~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, family = binomial(link = "logit"), data=bio)
glm2 <- glm(cmc~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, family = gaussian, data=bio)
logit2 <- glm(cmc~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, family = binomial(link = "logit"), data=bio)
inter <- glm(general~cmc_chair_connection_current+combat_post_1949+combat_post_1949*cohort_decade+college+participated_long_march+commissar+minority+parent_CCP_leader+rural, family = gaussian, data=bio)

inter %>% tidy() %>% kable()

GIM(inter, full = F)
```


## Mattingly's Model 1
```{r}
GIM(glm1, full = F)
```

## Mattingly's Model 2: Misspecified
```{r}
GIM(glm2, full = F)
```

## Logit Model 1
```{r}
GIM(logit1, full = F)
```

## Logit Model 2
```{r}
GIM(logit2, full = F)
```

## Subset to Mao and Deng Era: Some key variables are no longer significant, only 116 out of 779 samples
```{r}
deng1 <- glm(general~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, data=bio, family = gaussian, subset=post_deng==0)
deng2 <- glm(cmc~cmc_chair_connection_current+combat_post_1949+college+participated_long_march+commissar+minority+parent_CCP_leader+rural+cohort_decade, data=bio, family = gaussian, subset=post_deng==0)

deng1 %>% tidy() %>% kable()
deng2 %>% tidy() %>% kable()
```

# Change the coding of domestic threats
##1) social uprising
```{r}

##2008-2011
panel_year$domesticnew <- 0
panel_year$domesticnew [panel_year$year==2008|panel_year$year==2009|panel_year$year==2010|panel_year$year==2011] <- 1

##table
lm1 <- plm(general_varying~cmc_connection_varying*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew++as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant","age" , "college", "participated_long_march", "commissar", "combat_post_1949", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("domestic_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("domestic Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
         covariate.labels = c("Career Tie to Current CMC Chairman", "Period of domestic Threat", "Career Tie to Chairman X domestic Threat"),
         title = "Promotion during periods of domestic threat.",
         star.char = c("+", "*", "**"),
         notes.append=FALSE,
         notes = c("Robust standard errors are clustered by individual.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_2(uprising2008-2011).tex")

```

```{r}
##2014-2017
panel_year$domesticnew <- 0
panel_year$domesticnew [panel_year$year==2014|panel_year$year==2015|panel_year$year==2016|panel_year$year==2017] <- 1

##table
lm1 <- plm(general_varying~cmc_connection_varying*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew++as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant","age" , "college", "participated_long_march", "commissar", "combat_post_1949", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("domestic_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("domestic Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
         covariate.labels = c("Career Tie to Current CMC Chairman", "Period of domestic Threat", "Career Tie to Chairman X domestic Threat"),
         title = "Promotion during periods of domestic threat.",
         star.char = c("+", "*", "**"),
         notes.append=FALSE,
         notes = c("Robust standard errors are clustered by individual.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_3(uprising2014-2017).tex")

```

```{r}

##2019-2021
panel_year$domesticnew <- 0
panel_year$domesticnew [panel_year$year==2019|panel_year$year==2020|panel_year$year==2021] <- 1

##table
lm1 <- plm(general_varying~cmc_connection_varying*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew++as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant","age" , "college", "participated_long_march", "commissar", "combat_post_1949", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("domestic_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("domestic Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
         covariate.labels = c("Career Tie to Current CMC Chairman", "Period of domestic Threat", "Career Tie to Chairman X domestic Threat"),
         title = "Promotion during periods of domestic threat.",
         star.char = c("+", "*", "**"),
         notes.append=FALSE,
         notes = c("Robust standard errors are clustered by individual.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_4(uprising2019-2021).tex")
```

#2) intra-elite tension
```{r}
##1997,2002,2007,2012
panel_year$domesticnew <- 0
panel_year$domesticnew [panel_year$year==1997|panel_year$year==2002|panel_year$year==2007|panel_year$year==2012] <- 1

##table
lm1 <- plm(general_varying~cmc_connection_varying*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew+as.factor(year)*cohort_decade,  data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm4 <- plm(general_varying~cmc_connection_varying*domesticnew+age+combat_post_1949*domesticnew+college*domesticnew+participated_long_march*domesticnew+commissar*domesticnew+minority*domesticnew+parent_CCP_leader*domesticnew+rural*domesticnew++as.factor(year)*as.factor(birth_year),  data=panel_year, model="within")

cluster.fit4 <- coeftest(lm4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, lm4, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]), 
          omit=c("cohort_decade", "year", "Constant","age" , "college", "participated_long_march", "commissar", "combat_post_1949", "minority", "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          label=c("domestic_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("domestic Threat X Controls", "", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year FE X Birth decade FE", "", "", "$\\checkmark$", ""),
                         c("Year FE X Birth year FE", "", "", "", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
         covariate.labels = c("Career Tie to Current CMC Chairman", "Period of domestic Threat", "Career Tie to Chairman X domestic Threat"),
         title = "Promotion during periods of domestic threat.",
         star.char = c("+", "*", "**"),
         notes.append=FALSE,
         notes = c("Robust standard errors are clustered by individual.$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_5(intra-elite).tex")

```


# Change the coding of foreign threats
```{r warning=F}
panel_year$foreign_close <- 0
panel_year$foreign_close[panel_year$year==1979|panel_year$year==1980|panel_year$year==1981|panel_year$year==1982|panel_year$year==1983|panel_year$year==1984|panel_year$year==1996|panel_year$year==2017|panel_year$year==2021] <- 1

panel_year$foreign_far <- 0
panel_year$foreign_far[panel_year$year==1989|panel_year$year==1990|panel_year$year==1996|panel_year$year==1999|panel_year$year==2000|panel_year$year==2001|panel_year$year==2002] <- 1

panel_year$foreign_all <- 0
panel_year$foreign_all[panel_year$foreign_close==1|panel_year$foreign_far==1] <- 1

lm1 <- plm(general_varying~combat_post_1949*foreign_close+as.factor(year),  data=panel_year, model="within")

cluster.fit1 <- coeftest(lm1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm2 <- plm(general_varying~combat_post_1949*foreign_far+as.factor(year),  data=panel_year, model="within")

cluster.fit2 <- coeftest(lm2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

lm3 <- plm(general_varying~combat_post_1949*foreign_all+as.factor(year), data=panel_year, model="within")

cluster.fit3 <- coeftest(lm3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))


stargazer(lm1, lm2, lm3, type = "latex", 
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"]),
          omit="year",
          omit.stat=c("f", "ser"),
          label=c("foreign_threat"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Clusters", rep(length(unique(panel_year$unique_id)), 4))),
          covariate.labels = c("Neighboring Threat", "American Threat", "All Foreign Threat", "Combat Experience X Neighboring Threat", "Combat Experience X American Threat", "Combat Experience X All Threat"),
          title = "Promotion during periods of foreign threat",
          star.char = c("+", "*", "**"),
          notes.append=FALSE,
          notes = c("$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_4.tex")
```

# Reversing Treatment Variables
```{r}
################# Without Controls  ###################

#Professionalism in Domestic Threat 
lm1a <- plm(general_varying~combat_post_1949*domestic+as.factor(year),  data=panel_year, model="within")
summary(lm1a)

#Loyalty in Domestic Threat 
lm1 <- plm(general_varying~cmc_connection_varying*domestic+as.factor(year),  data=panel_year, model="within")
summary(lm1)

#Loyalty in Foreign Threat 
lm1d<-plm(general_varying~cmc_connection_varying*foreign+as.factor(year),  data=panel_year, model="within")
summary(lm1d)

#Professionalism in Foreign Threat 
lm1f <- lm1 <- plm(general_varying~combat_post_1949*foreign+as.factor(year),  data=panel_year, model="within")
summary(lm1f)

#################With Controls #############

#Professionalism in Domestic Threat 
lm2a <- plm(general_varying~combat_post_1949*domestic+age+cmc_connection_varying*domestic+college*domestic+participated_long_march*domestic+commissar*domestic+minority*domestic+parent_CCP_leader*domestic+rural*domestic+as.factor(year),  data=panel_year, model="within")
summary(lm2a)

#Loyalty in Domestic Threat 
lm2 <- plm(general_varying~cmc_connection_varying*domestic+age+combat_post_1949*domestic+college*domestic+participated_long_march*domestic+commissar*domestic+minority*domestic+parent_CCP_leader*domestic+rural*domestic+as.factor(year),  data=panel_year, model="within")
summary(lm2)

#Loyalty in Foreign Threat 
lm2d <-plm(general_varying~cmc_connection_varying*foreign+age+combat_post_1949*foreign+college*foreign+participated_long_march*foreign+commissar*foreign+cmc_connection_varying*foreign+parent_CCP_leader*foreign+rural*foreign+as.factor(year),  data=panel_year, model="within")
summary(lm2d)

#Professionalism in Foreign Threat 
lm2f <- plm(general_varying~combat_post_1949*foreign+age+cmc_connection_varying*foreign+college*foreign+participated_long_march*foreign+commissar*foreign+combat_post_1949*foreign+parent_CCP_leader*foreign+rural*foreign+as.factor(year),  data=panel_year, model="within")
summary(lm2f)



##################Make the Table ###################
#Stargazer table 
library(stargazer)

stargazer(lm1a,lm1d,lm2a,lm2d, type = "latex",
          title = "Regression of Reversed Dependent Variables",
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          omit=c("cohort_decade", "year", "Constant","age" , "college", 
                 "participated_long_march", "commissar", "minority", 
                 "participated_long_march", "rural", "parent_CCP_leader"),
          omit.stat=c("f", "ser"),
          add.lines=list(c("Individual fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Year fixed effects", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$", "$\\checkmark$"),
                         c("Domestic Threats X Controls", "","", "$\\checkmark$", ""),
                         c("Foreign Threats X Controls", "", "", "","$\\checkmark$")),
          covariate.labels = c("Domestic Threat", "Career Tie to CMC Chairman", "Foreign Threat", "Combat Experience X Domestic Threat", "Career Tie to CMC Chairman X Domestic Threat",
                               "Combat Experience X Foreign Threat", "Career Tie to CMC Chairman X Foreign Threat"),
          star.char = c("+", "*", "**"),
          notes.append = FALSE,
          notes = c("$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "mod.txt")


```

# DID 
```{r}
panel_year$time1 <- ifelse(as.numeric(panel_year$year) >= 11, 1, 0)
panel_year$did1 <- panel_year$time1 * panel_year$cmc_chair_connection
panel_year$did2 <- panel_year$time1 * panel_year$combat_post_1949

panel_year$time2 <- ifelse(as.numeric(panel_year$year) >= 34, 1, 0)
panel_year$did3 <- panel_year$time2 * panel_year$cmc_chair_connection
panel_year$did4 <- panel_year$time2 * panel_year$combat_post_1949

didreg1 <- lm(general_varying ~ time1 + cmc_chair_connection + did1, data = panel_year)
cluster.fit1 <- coeftest(didreg1, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

didreg2 <- lm(general_varying ~ time1 + combat_post_1949 + did2, data = panel_year)
cluster.fit2 <- coeftest(didreg2, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

didreg3 <- lm(general_varying ~ time2 + cmc_chair_connection + did3, data = panel_year)
cluster.fit3 <- coeftest(didreg3, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

didreg4 <- lm(general_varying ~ time2 + combat_post_1949 + did4, data = panel_year)
cluster.fit4 <- coeftest(didreg4, vcov = function(x) 
  plm::vcovHC(x, method = 'white1', cluster = 'group', type = 'HC3'))

stargazer(didreg1, didreg2, didreg3, didreg4, type = "latex",
          se = list(cluster.fit1[,"Std. Error"], cluster.fit2[,"Std. Error"],
                    cluster.fit3[,"Std. Error"], cluster.fit4[,"Std. Error"]),
          omit = "year",
          omit.stat = c("f", "ser"),
          label=c("did"),
          order = c(1, 2, 3, 5, 4, 6, 7, 8),
          covariate.labels = c("Cutoff: 1989", "Cutoff: 2012", "Career Tie to Current CMC Chairman", "Combat Experience", "Career Tie X Cutoff 1989", "Combat Experience X Cutoff 1989", "Career Tie X Cutoff 2012", "Combat Experience X Cutoff 2012"),
          dep.var.labels = c("Promoted to General (Mean: 0.295)"),
          title = "Difference-in-Difference Tests Using 1989 or 2012 as Cutoff",
          star.char = c("+", "*", "**"),
          notes.append=FALSE,
          notes = c("$^{+}$p$<$0.1; $^{*}$p$<$0.05; $^{**}$p$<$0.01}"),
          out = "Table_5.tex")
```

